// Java VM for small microcontrollers
//
// (c) 2012, Digital Six Laboratories LLC
// All Rights Reserved
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
// and associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Original work based on NanoVM (http://www.harbaum.org/till/nanovm/index.shtml) for AVR microcontroller
//=========================================================================================================
// 
// vmfile.h


#ifndef NVMFILE_H
#define NVMFILE_H

#include "vmtypes.h"
#include "vm.h"

typedef struct 
{
	U8 super;
	U8 fields;
} tVmFileClassHeader;

typedef struct 
{
	U16 code_index;
	U16 id;           // class and method id
	U8 flags;
	U8 args;
	U8 max_locals;
	U8 max_stack;
} tVmFileMethodHeader;

typedef struct 
{
	U32 magic_feature;    // old 32 bit magic is replaced by 8 bit magic and 24 feauture bits
	U8 version;
	U8 methods;          // number of methods in this file
	U16 main;             // index of main method
	U16 constant_offset;
	U16 string_offset;
	U16 method_offset;
	U8 static_fields;
	tVmFileClassHeader* class_hdr;
} tVmFileHeader;

// marker that indicates, that a method is a classes init method
#define FLAG_CLINIT 1

extern U8 vmFileConstantCount;

void   VmFileSaveApp(U16 index, U8 *buffer, U16 size);

BOOL VmFileInitialize(void);
void   VmFileJumpToMain(void);
void   VmFileWriteByte(void *addr, U8 writedata);

void   *VmFileGetAddress(U16 ref);
U8  VmFileGetClassFieldCount(U8 index);
U8  VmFileGetStaticFieldCount(void);
U32  VmFileGetConstant(U8 index);

void   VmFileRead(void *dst, void *src, U16 len);
U8  VmFileRead08(void *addr);
U16  VmFileRead16(void *addr);
U32  VmFileRead32(void *addr);
U8  VmFileGetMethodByClassAndId(U8 class, U8 id);

tVmFileMethodHeader xdata *VmFileGetMethodHeader(U16 index);

#define VMSETFILEFLAG(a)     (void*)(((PTR)a) | NVMFILE_FLAG)
#define VMISFILEFLAGSET(a)   (((PTR)a) & NVMFILE_FLAG)
#define VMFILLADDR(a)    (void*)(((PTR)a) & ~NVMFILE_FLAG)

#endif // NVMFILE_H
